package org.vectrics.skill.struts;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.struts.tag.PeopleComboTag;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.user.PersonPrivilegeGroupType;
import org.vectrics.user.PersonSearchCriteria;



public class EvaluatorComboTag extends PeopleComboTag {
	public static final long serialVersionUID = 16737439455742L;
	
    static private transient Logger log = Logger.getLogger(EvaluatorComboTag.class);
    private String evaluationId = null;
    private String domainId = null;
    public String PROPERTY_NAME = "evaluatorId";
	
    public EvaluatorComboTag() {
    }
    
	protected Collection findPersons() {
		log.debug("findPersons() called");
		PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
		
		if ((this.domainId == null) || (this.domainId.length() == 0)) {
			throw new SystemException("domainId was not specified");
		} else {
			Domain domain = CoreServiceLocator.getSystemService().findDomain(new Long(this.getDomainId()));
			criteria.addDomain(domain);
		}
		PersonPrivilegeGroupType groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("hiring.mgr");
		criteria.addPersonPrivilegeGroup(groupType);
		groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("recruiter");
		criteria.addPersonPrivilegeGroup(groupType);
		groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("recruiter.manager");
		criteria.addPersonPrivilegeGroup(groupType);
		groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("hiring.manager");
		criteria.addPersonPrivilegeGroup(groupType);
		
		Collection persons = CoreServiceLocator.getPersonService().findPersons(criteria, 0, 50);
		return(persons);
	}

	protected boolean getAddNoPersonOption() {
		return(true);
	}

	protected Long getSelectedPersonId() throws DataException{
		Long selectUserId = null;
		if (this.evaluationId != null) {
			UserEvaluation eval = RecruitServiceLocator.getSkillProvider().findEvaluationById(new Long(evaluationId));
			if (eval != null) {
			    if (eval.getLeadPerson() != null)
			        selectUserId = eval.getLeadPerson().getId();
			}
		}
		
		return(selectUserId);
	}

	public String getProperty() {
		return(PROPERTY_NAME);
	}
	
	/**
	 * @return Returns the evaluationId.
	 */
	public String getEvaluationId() {
		return evaluationId;
	}
	
	/**
	 * @param evaluationId The evaluationId to set.
	 */
	public void setEvaluationId(String evaluationId) {
		this.evaluationId = evaluationId;
	}

	/**
	 * @return Returns the domainId.
	 */
	public String getDomainId() {
		return domainId;
	}

	/**
	 * @param domainId The domainId to set.
	 */
	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}


}
